package cn.stylefeng.guns.modular.cb.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbPosition;
import cn.stylefeng.guns.modular.cb.model.params.*;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerStaffResult;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerStudentsResult;
import cn.stylefeng.guns.modular.cb.model.result.CbPositionResult;
import cn.stylefeng.guns.modular.cb.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 控制器
 * 招聘职位
 * @author wj
 * @Date 2021-01-05 22:32:22
 */
@RestController
@RequestMapping("/api/position")
public class CbPositionController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(CbPositionController.class);

    @Autowired
    private CbPositionService cbPositionService;

    @Autowired
    private CbFocusCollectionService cbFocusCollectionService;

    @Autowired
    private CbCustomerStudentsService cbCustomerStudentsService;

    @Autowired
    private CbCustomerStaffService cbCustomerStaffService;

    @Autowired
    private CbResumeDeliveryService cbResumeDeliveryService;

    /**
     * 新增接口
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/addItem")
    public ResponseData addItem(CbPositionParam cbPositionParam) {
        this.cbPositionService.add(cbPositionParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/editItem")
    public ResponseData editItem(CbPositionParam cbPositionParam) {
        this.cbPositionService.update(cbPositionParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/delete")
    public ResponseData delete(CbPositionParam cbPositionParam) {
        this.cbPositionService.delete(cbPositionParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/detail")
    public ResponseData detail(CbPositionParam cbPositionParam) {
        CbPosition detail = this.cbPositionService.getById(cbPositionParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/list")
    public LayuiPageInfo list(CbPositionParam cbPositionParam) {
        return this.cbPositionService.findPageBySpec(cbPositionParam);
    }

    /**
     * 查询列表
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/list2")
    public LayuiPageInfo list2(CbPositionParam cbPositionParam) {
        return this.cbPositionService.findPageBySpec2(cbPositionParam);
    }

    /**
     * 查询职位类型
     * @param cbPositionParam
     * @return
     */
    @PostMapping("/list_name")
    public ResponseData listByName(CbPositionParam cbPositionParam){
        List<CbPositionResult> list = cbPositionService.positionByNameList(cbPositionParam);
        Set<String> set = new HashSet<>();
        for(CbPositionResult cpr: list){
            set.add(cpr.getName());
        }
        return ResponseData.success(set);
    }

    /**
     * 查询发布人
     * @param cbPositionParam
     * @return
     */
    @PostMapping("/list_user")
    public ResponseData listByCreateUser(CbPositionParam cbPositionParam){
        List<CbPositionResult> list = cbPositionService.customList2(cbPositionParam);
        Iterator<CbPositionResult> iterator = list.iterator();
        while (iterator.hasNext()){
            CbPositionResult item = iterator.next();
            if (item==null) {
                iterator.remove();
            }
        }
        return ResponseData.success(list);
    }

    /**
     * 职位信息
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/position_info")
    public ResponseData positionInfo(CbPositionParam cbPositionParam) {
        CbPositionResult detail = this.cbPositionService.getCbPositionInfo(cbPositionParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 职位详情
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/position_detail")
    public ResponseData positionDetail(CbPositionParam cbPositionParam) {
        CbPositionResult detail = new CbPositionResult();
        try{
            detail = this.cbPositionService.getCbPositionDetail(cbPositionParam.getId());

            //该职位是否收藏
            LoginUser user = LoginContextHolder.getContext().getUser();
            CbCustomerStudentsParam studentParam = new CbCustomerStudentsParam();
            studentParam.setCreateUser(user.getId());
            //普通用户会收藏职位
            CbCustomerStudentsResult studentsResult = cbCustomerStudentsService.getStudent(studentParam);
            if (studentsResult!=null && !StringUtils.isEmpty(studentsResult.getName())){
                CbFocusCollectionParam param = new CbFocusCollectionParam();
                param.setObjectId(detail.getId());
                param.setUserId(studentsResult.getId());
                param.setType(2);
                Integer count = cbFocusCollectionService.getFocusCollectionCount(param);
                detail.setCollection(count);
            }

            //是否已经投递
            Long createUser = cbPositionParam.getCreateUser();
            CbResumeDeliveryParam cbResumeDeliveryParam = new CbResumeDeliveryParam();
            cbResumeDeliveryParam.setCreateUser(createUser);
            cbResumeDeliveryParam.setPositionId(cbPositionParam.getId());
            Integer count = cbResumeDeliveryService.getCountByUser(cbResumeDeliveryParam);
            detail.setRdCount(count);
        }catch (Exception e) {
            logger.error("position_detail error: ", e);
        }
        return ResponseData.success(detail);
    }

    /**
     * 职位信息
     *
     * @author wj
     * @Date 2021-01-05
     */
    @PostMapping("/list_position_staff")
    public ResponseData listPositionByStaff(String schoolNo, String companyId) {
        if (!StringUtils.isEmpty(schoolNo)){
            CbCustomerStaffParam param = new CbCustomerStaffParam();
            List<CbCustomerStaffResult> staffList = cbCustomerStaffService.companyByAlumni(schoolNo);
            StringBuilder sb = new StringBuilder();
            for(CbCustomerStaffResult staffResult : staffList) {
                sb.append(staffResult.getCompanyId()).append(",");
            }
            companyId = sb.toString();
        }
        List<CbPositionResult> list = cbPositionService.listPositionByStaff(companyId);
        return ResponseData.success(list);
    }
}


